//
//  xianViewController.swift
//  BusinessIOS
//
//  Created by Rain on 2019/1/5.
//  Copyright © 2019 XingJian Li. All rights reserved.
//

import UIKit
import Alamofire
class xianViewController: UIViewController {
    var xianArray = [xianModel]()
    
    var xian:String!
    var tableView: UITableView!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        let manager = UserDefaults()
        self.xian = manager.string(forKey:"newsClassId")  //根据关键字取值

        if  self.xian == nil {
            
           
            
        }
        else{
            
             self.loadData()
            
            
        }
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
      
        
        self.view.backgroundColor = UIColor.white
        tableView = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 180), style: .plain )
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorInset = UIEdgeInsets.zero
        //        tableView.separatorStyle = .none
        tableView.backgroundColor = bgGrayColor
        tableView.showsHorizontalScrollIndicator = false
        tableView.showsVerticalScrollIndicator = false
        //tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0)//去除表格上放多余的空隙
        //tableView.tableHeaderView = UIView.init()
        tableView.tableFooterView = UIView.init()
        self.view.addSubview(tableView)
        
       
        
        
        
    }
    

  


  

    
    func loadData() {
        
        self.xianArray.removeAll()
        
        let quurlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/street/newsClassList"

        let json = "{\"newsClassId\":\"\(self.xian!)\"}"
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        let url = URL(string: quurlString)!
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        Alamofire.request(request).responseJSON {(response) in

            print("县=======\(response)")
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array

                for dataDic in  array! {
                    let model =   xianModel()
                    model.newsClassId = dataDic["newsClassId"].string

                    model.newsClassName = dataDic["newsClassName"].string



                    self.xianArray .append(model)


                }
                
                 UserDefaults.standard.removeObject(forKey: "newsClassId")

                self.tableView.reloadData()


            }else{

                return
            }
        }

    }
    
    
}
extension xianViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.xianArray.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let model:xianModel = self.xianArray[indexPath.row]
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            
            cell = UITableViewCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        }
        cell?.textLabel?.text = model.newsClassName
        return cell!
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        
        let cell1:UITableViewCell = tableView.cellForRow(at: indexPath)!

        let dic: [String: String] = ["title": (cell1.textLabel?.text)!]
        
        NotificationCenter.default.post(name: NSNotification.Name(YZUpdateMenuTitleNote), object: self, userInfo:dic)
        
    }
    
    
  
    
}
